#!/bin/bash

# Slackware build script for mp3splt-gtk

# Copyright 2008-2013 Roberto Neri, Palermo, Italy
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 20211121 bkw: note to self: upstream moved to github and
# combined all 3 projects (libmp3splt, mp3splt, mp3splt-gtk) into
# one repo. Probably should combine the SlackBuilds, too. See
# https://github.com/mp3splt/mp3splt

# 20211121 bkw: BUILD=2
# - new-style icons.
# - don't pollute /usr/share/pixmaps with internal-use-only images.
# - GSTREAMER=no and audacious support are gone. I didn't do this,
#   rworkman did, just reminding myself why: mp3splt-gtk doesn't
#   support our newer version of audacious. Sorry, Arthur.

# 20150304 bkw:
# - Upstream accepted my patch, in somewhat modified form, as revision
#   1509 in their SVN repo. Update this build to use their patch instead
#   of mine.
# - Bump BUILD.

# 20150107 bkw:
# - Updated for v0.9.2
# - Added GSTREAMER=no option, as requested by Arthur Fontolan.
# - Added default_player.diff: if GSTREAMER=no, the default player
#   will be audacious. Sent patch to upstream too.

# 20140819 bkw:
# - Updated for v0.9.1a
# - Use make install-strip instead of find/xargs/strip
# - Get rid of redundant/empty TODO and INSTALL from /usr/doc
# - Don't waste time building doxygen docs, since we don't install them

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=mp3splt-gtk
VERSION=${VERSION:-0.9.2}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# patch made with 'svn diff -c 1509' in a checked-out repo.
patch -p1 < $CWD/default_player-r1509.diff

sed -i '/Icon/s,=.*,='$PRGNAM',' $PRGNAM.desktop.in

# Disabled audacious support, upstream mp3splt-gtk does not yet support audacious-3.6
CFLAGS="$SLKCFLAGS -fcommon" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --enable-gstreamer \
  --disable-audacious \
  --disable-gnome \
  --disable-scrollkeeper \
  --disable-doxygen_doc \
  --build=$ARCH-slackware-linux

make appicondir=/usr/share/$PRGNAM
make install-strip DESTDIR=$PKG appicondir=/usr/share/$PRGNAM

gzip -9 $PKG/usr/man/man?/*

HICOLOR=$PKG/usr/share/icons/hicolor
SVG=src/mp3splt-gtk_ico.svg
for px in 16 32 48 64 128; do
  size=${px}x${px}
  dir=$HICOLOR/$size/apps
  mkdir -p $dir
  convert -resize $size $SVG $dir/$PRGNAM.png
done

mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS ChangeLog COPYING NEWS README $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
